package com.zoe.vhr.controller.system.basic;

import com.zoe.vhr.model.Department;
import com.zoe.vhr.service.DepartmentService;
import com.zoe.vhr.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Author Zoe.Zhang
 * @Date: 2021/08/07
 * @Description 部门接口
 */
@RestController
@RequestMapping("/system/basic/department")
@Validated
public class DepartmentController {
    @Autowired
    DepartmentService departmentService;

    @GetMapping("/listAllDepartmentsWithChildren")
    public R listAllDepartmentsWithChildren() {
        List<Department> departments = departmentService.listAllDepartmentsWithChildren();
        return R.success(departments);
    }

    @GetMapping("/listAllDepartmentName")
    public R listAllDepartmentName() {
        List<Department> departmentNames = departmentService.listAllDepartmentName();
        return R.success(departmentNames);
    }

    @PostMapping("/savaDepartment")
    public R savaDepartment(@Valid @RequestBody Department department) {
        departmentService.savaDepartment(department);
        if (department.getResult() == 1) {
            return R.success(department, "添加成功");
        }
        return R.failed("添加失败");
    }

    @DeleteMapping("/deleteDepartment/{deptId}")
    public R deleteDepartment(@PathVariable("deptId") Integer deptId) {
        Department department = new Department();
        department.setId(deptId);
        departmentService.deleteDepartment(department);
        if (department.getResult() == -2) {
            return R.failed("删除失败,当前部门下还有子部门!!");
        } else if (department.getResult() == -1) {
            return R.failed("删除失败,当前部门下还有员工");
        } else if (department.getResult() == 1) {
            return R.success("删除成功");
        }
        return R.failed("删除失败");
    }
}
